package com.dinzeer.slashblade_useful_addon.events;

import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import mods.flammpfeil.slashblade.init.SBItems;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.CraftingMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class ToolTipEvents {

    // 合成物品事件处理
    @SubscribeEvent
    public static void onItemCrafted(PlayerEvent.ItemCraftedEvent event) {
        ItemStack stack = event.getCrafting();
        Player player = event.getEntity();

        // 检查是否是目标物品
        if (stack.getItem().asItem() instanceof ItemSlashBlade) {
            CompoundTag nbt = stack.getOrCreateTag();
            // 使用玩家名称作为键存储到NBT
            nbt.putString("Crafter", player.getName().getString());
        }
    }

    // 工具提示事件处理
    @SubscribeEvent
    public static void onTooltip(ItemTooltipEvent event) {
        ItemStack stack = event.getItemStack();
        Player player = event.getEntity();

        if (stack.getItem().asItem() instanceof ItemSlashBlade) {
            // 判断是否在合成台结果槽
            if (isInCraftingOutput(player, stack)) {
                event.getToolTip().add(Component.literal("此为你所造之刃，拿出它吧").withStyle(ChatFormatting.GOLD));
            } else {

                CompoundTag nbt = stack.getTag();
                String crafter = (nbt != null && nbt.contains("Crafter"))
                        ? nbt.getString("Crafter")
                        : "世界意志";
               String[] s= SlashBladeUtil.getTranslationKey(stack).split("\\.");
                if (crafter.equals("世界意志")){
                    if (s.length>1&&s[1].equals("last_smith")){
                    crafter="无名刀匠";
                    }
                }
                event.getToolTip().add(Component.literal("锻刀人: " + crafter).withStyle(ChatFormatting.GOLD));
            }
        }
        if (stack.getItem().asItem() == SBItems.proudsoul_trapezohedron) {
            CompoundTag nbt = stack.getTag();
            if (nbt != null && nbt.contains("killcount")){
            event.getToolTip().add(Component.literal("杀敌数: " + nbt.getInt("killcount")).withStyle(ChatFormatting.GOLD));
            }
        }
    }

    // 判断物品是否在合成台输出槽
    private static boolean isInCraftingOutput(Player player, ItemStack stack) {
        if (player == null) return false;

        // 检查打开的容器是否是合成台
        if (player.containerMenu instanceof CraftingMenu) {
            // 获取合成台结果槽（索引0）
            Slot resultSlot = player.containerMenu.slots.get(0);
            // 比较物品是否相同且数量匹配
            return ItemStack.matches(stack, resultSlot.getItem());
        }
        return false;
    }

}
